package com.zy.mc_module.mvp.presenter;

import android.content.Context;
import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.bean.*;
import com.zy.mc_module.mvp.contract.UserContract.View;
import com.zy.mc_module.mvp.model.UserModel;
import com.zy.mc_module.utils.UserUtils;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.RxTransform;
import com.zy.mc_module.utils.retrofit.error.ThdException;

/**
 * Created on 18/12/3
 */
public class UserPresenter extends BasePresenter<View> {

    public static final int partnerSignInConfig = 111;
    public static final int partnerSignIn = 222;
    private UserModel userModel;

    public UserPresenter() {
        userModel = new UserModel();
    }

    /***
     * 获取用户信息
     */
    public void getUserInfo(Context context) {
//        getInviteInfo(context);
        if (UserUtils.isLogin()) {
            userModel.getUserInfo().compose(RxTransform.schedule())
                    .subscribe(new RxObserver<BaseBean<UserBean>>(this, context) {
                        @Override
                        protected void onBaseNext(BaseBean<UserBean> beanBaseBean) {
                            getView().onUserInfoSuccess(beanBaseBean.data);
                        }

                        @Override
                        protected void onBaseError(ThdException e) {
                            getView().onError(e);
                        }
                    });
        } else {
            getView().onUserInfoSuccess(UserUtils.getUser());
        }
    }

    /***
     * 编辑用户信息
     */
    public void editUserInfo(Context context, String url, Object... objects) {
        userModel.editUserInfo(url, objects).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context) {
                    @Override
                    protected void onBaseNext(BaseBean baseBean) {
                        // 编辑用户成功,不显示
                        // getView().onEditSuccess();
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        if (isViewAttached()) {
                            getView().onError(e);
                        }
                    }
                });
    }
}
